<?php
/**
 * Created by PhpStorm.
 * User: 97438
 * Date: 2019/9/17
 * Time: 15:01
 */

namespace app\controller\api;
use think\facade\Cache;
use app\ApiController;
use app\model\UserModel;
use app\model\VerifyCodeModel;

class UserController extends ApiController
{
    /**
     * 登录
     */
    public function login(){
        try{
            $mob = isset($_REQUEST['mob']) ? $_REQUEST['mob'] : '';
            $pwd = isset($_REQUEST['pwd']) ? $_REQUEST['pwd'] : '';
            if(empty($mob)){
                return $this->echoErr(array(),'手机号不能为空');
            }
            if(empty($pwd)){
                return $this->echoErr(array(),'请输入密码');
            }
            $userModel = new UserModel();
            $user = $userModel->login($mob,$pwd);
            if(empty($user)){
                return $this->echoErr(array(),'用户名或密码错误');
            }
            $data = array(
                'token'=>$user['token'],
                'loginTime' => $user['loginTime'],
            );
            return $this->echoOk($data,'登录成功');
        } catch (Exception $e){
            return $this->echoErr(array(),'登录失败,请稍后重试');
        }
    }

    /**
     * 发送短信验证码
     */
    public function sendRegistCode(){
        try{
            $mob = isset($_REQUEST['mob']) ? $_REQUEST['mob'] : '';
            $verifyCodeModel = new VerifyCodeModel();
            $verifyCode = $verifyCodeModel->send($mob,1);
            if(empty($verifyCode)){
                $error = empty($verifyCodeModel->error) ? '发送失败' : $verifyCodeModel->error;
                return $this->echoErr(array(),$error);
            }
            $data = array(
                //'code' => $verifyCode['code'],  //为了方便测试把验证码返回
            );
            return $this->echoOk($data,'发送成功');
        } catch (Exception $e){
            return $this->echoErr(array(),'发送失败');
        }
    }

    /**
     * 注册
     */
    public function regist(){
        try{
            $mob = isset($_REQUEST['mob']) ? $_REQUEST['mob'] : '';
            $pwd = isset($_REQUEST['pwd']) ? $_REQUEST['pwd'] : '';
            $code = isset($_REQUEST['code']) ? $_REQUEST['code'] : '';
            if(empty($mob)){ //可以加上手机号正则验证
                return $this->echoErr(array(),'手机号不能为空');
            }
            if(empty($code)){ //可以加上手机号正则验证
                return $this->echoErr(array(),'请输入短信验证码');
            }
            if(empty($pwd) || strlen($pwd) < 6){  //可以根据不同的要求进行正则匹配判断
                return $this->echoErr(array(),'密码不能少于6个字符');
            }
            //短信验证码
            $verifyCodeModel = new VerifyCodeModel();
            $verifyCodeStatus = $verifyCodeModel->verify($mob,$code,1);
            if(!$verifyCodeStatus){
                $cache = Cache::get("verify:code:".$mob.":type:1");
                $error = empty($verifyCodeModel->error) ? '验证码有误' : $verifyCodeModel->error;
                return $this->echoErr(array('11'=>$cache),$error);
            }
            $userModel = new UserModel();
            $user = $userModel->regist($mob,$pwd);
            if(empty($user)){
                $error = empty($userModel->error) ? '注册失败' : $userModel->error;
                return $this->echoErr(array(),$error);
            }
            $data = array(
                //'user' => $user,
            );
            return $this->echoOk($data,'注册完成');
        } catch (Exception $e){
            $this->echoErr(array(),'注册失败,请稍后重试');
        }
    }

    /**
     * 获取用户信息
     */
    public function getInfo(){
        if(empty($this->loginUser)){
            return $this->echoOk(array(),'未登录');
        }
        $user = $this->loginUser;
        $data = array(
            'userId' => $user->userId,
            'mob' => $user->mob,
            'loginTime' => $user->loginTime,
            'registerDate' => $user->registerDate,
        );
        return $this->echoOk($data,'获取成功');
    }
}